﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using NesGfx;
namespace cnetmegamanprj
{
    public partial class Form1 : Form
    {
        public static MegaMan mm;

        int level=0;

        public Form1()
        {
            InitializeComponent();
        }

        static string romfn;
        public static void Save()
        {
            mm.rom.Save(romfn);
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                this.KeyPreview = true;
                label4.Text = "";
                var args = Environment.GetCommandLineArgs();
                if (args.Count() == 2)
                {
                    romfn = args[1];
                    mm = MegaManLoader.load(romfn);
                    if(mm.testFunc())
                        this.Close();

                    postLoadRom();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Exception: " + ex.Message);
                this.Close();
            }
        }

        void postLoadRom()
        {
            label2.Text = mm.GameNumber.ToString();
            label4.Text = romfn;
            new WorldEdit(mm).ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            new tsaPreset(mm.loadTsa(level)).Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            new TileEditor(mm.getTileEditData()).Show();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            new WorldEdit(mm).ShowDialog();
            //
        }

        private void button4_Click(object sender, EventArgs e)
        {            
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void button5_Click(object sender, EventArgs e)
        {
            new SimpleTileDisplay(mm).Show();
        }

        private void label4_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(label4.Text))
            {
                ShowLocation(label4.Text);
            }
        }
        

        void ShowLocation(string dirname_or_file)
        {
            var d = Directory.Exists(dirname_or_file) ? dirname_or_file : Path.GetDirectoryName(dirname_or_file);
            var arg = string.Format(@"""{0}""",  d);
            var osInfo = System.Environment.OSVersion;

            if (osInfo.Platform == PlatformID.MacOSX)
                System.Diagnostics.Process.Start("open", arg);
            else if (osInfo.Platform == PlatformID.Unix)
            {
                do
                {
                    var proc = System.Diagnostics.Process.Start("xdg-open", arg);
                    proc.WaitForExit();
                    if (proc.ExitCode == 0)
                        break;

                    proc = System.Diagnostics.Process.Start("gnome-open", arg);
                    proc.WaitForExit();
                    if (proc.ExitCode == 0)
                        break;

                    proc = System.Diagnostics.Process.Start("kde-open", arg);
                }
                while (false);
            }
            else
            {
                System.Diagnostics.Process.Start("explorer.exe", arg);
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            var ofn = new OpenFileDialog();
            if (ofn.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                loadrom(ofn.FileName);
            }
        }
        void loadrom(string fn)
        {
            mm = MegaManLoader.load(fn);
            if (mm.testFunc())
                this.Close();
            romfn = fn;
            postLoadRom();
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Escape)
                this.Close();
        }
    }
}
